package com.hongxun.design.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.CreateDistanceBean;
import com.hongxun.design.mytools.MyLog;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class CreateProjectAttributeAdapter extends BaseAdapter {
    private List<CreateDistanceBean> beanList;
    LayoutInflater inflater;
    Activity context;

    public CreateProjectAttributeAdapter(List<CreateDistanceBean> beanList, Activity context) {
        this.beanList = beanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public List<CreateDistanceBean> getData(){
        return beanList;
    }
    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private int index = -1;
    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);
        Holder holder = null;
        convertView = inflater.inflate(R.layout.listview_item_createattribute, null);
        holder = new Holder(convertView);
        convertView.setTag(holder);
        CreateDistanceBean bean;
        try {
            bean = beanList.get(position);
            //判断是否为输入框
            holder.create_attritem_name.setText(bean.getTitle());
            holder.create_attritem_write.setText(bean.getValue());
            holder.create_attritem_right.setText(bean.getCompany());
            holder.create_attritem_write.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View view, MotionEvent event) {

                    // 在TOUCH的UP事件中，要保存当前的行下标，因为弹出软键盘后，整个画面会被重画
                    // 在getView方法的最后，要根据index和当前的行下标手动为EditText设置焦点

                    if (event.getAction() == MotionEvent.ACTION_UP) {

                        index = position;

                    }

                    return false;

                }

            });
            holder.create_attritem_write.addTextChangedListener(bean.getTextWatcher());

        } catch (Exception e) {
            MyLog.L(MyApp.AppName + "适配错误：" + e.toString());
        }
        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (view instanceof EditText) {
                    //EditText设置为可焦点
                    EditText editText = (EditText) view;
                    editText.setFocusable(true);
                    editText.setFocusableInTouchMode(true);
                } else {

                }
                return false;

            }
        });
        holder.create_attritem_write.clearFocus();
        if(index!= -1 && index == position) {
            // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
            holder.create_attritem_write.requestFocus();

        }
        return convertView;

    }

    public class Holder {
        public TextView create_attritem_name, create_attritem_right;
        public EditText create_attritem_write;
        public RelativeLayout create_attritem_item_button;

        Holder(View convertView) {
            create_attritem_item_button = (RelativeLayout) convertView.findViewById(R.id.create_attritem_item_button);
            create_attritem_name = (TextView) convertView.findViewById(R.id.create_attritem_name);
            create_attritem_right = (TextView) convertView.findViewById(R.id.create_attritem_right);
            create_attritem_write = (EditText) convertView.findViewById(R.id.create_attritem_write);
        }
    }


}
